<?php namespace sacvoyage\proxy;

require_once 'sacvoyage/http/util.php';
use \sacvoyage\http\load_json;
use \sacvoyage\http\save_json;

require_once 'sacvoyage/util.php';

require_once 'sacvoyage/Object.php';
use \sacvoyage\Object;

class Collection {
	
	public function __construct($browser, $url) {
		$this->_browser=$browser;
		$this->_url=$url;
	}
	
	public function getOwner() {
		return null;
	}
	

	public function write($documents) {
		try {
			$result=new Object(array('status'=>'ok', 'written'=>array()));
			if(!is_enumerative_array($documents) && !$documents instanceof \Iterator) {
				$documents=array($documents);
			}
			
			$data='[';
			foreach($documents as $document) {
				if($data[strlen($data)-1]!='[') {
					$data.=',';
				}
				$data.=\sacvoyage\http\save_json($document);
				if(strlen($data)>1024*1024) { // write chunks no more than Megabyte each
					$data.=']';
					$headers=array('Content-Type'=>'application/json');
					$response=$this->_browser->request('POST', $this->_url,	$data, $headers);
					$response=\sacvoyage\http\load_json($response['body']);
					var_dump($response);
					if(/* $response && */$response->status=='ok') {
						$result->status='ok';
						$result->written=array_merge($result->written, $response->written);
						
					}		
					$data='[';
					$once=false;
				}
			}
			
			// copypaste, sorry
			if($data && $data!='[') {
				$data.=']';
				$headers=array('Content-Type'=>'application/json');
				$response=$this->_browser->request('POST', $this->_url,	$data, $headers);
				$response=\sacvoyage\http\load_json($response['body']);
				var_dump($response);
				if(/* $response && */$response->status=='ok') {
					$result->status='ok';
					$result->written=array_merge($result->written, $response->written);
				}		
			}
	
			return $result;
		} catch(\sacvoyage\http\JSONException $e) {
			error_log("unable to write documents to $this->_url: ".$e->getTraceAsString());
			echo("unable to write documents to $this->_url: ".$e->getTraceAsString());
			return array();
		}
	}
	
	public function read($id, $rev=null) {
		try {
			$url=rtrim($this->_url, '/').'/'.$id;
			if($rev) {
				$url.='~'.$rev;
			}
			$response=$this->_browser->request("GET", $url);
			return \sacvoyage\http\load_json($response['body']);
		} catch(\sacvoyage\http\JSONException $e) {
			error_log("unable to read document #($id, $rev) from $this->_url: ".$e->getTraceAsString());
			echo("unable to read document #($id, $rev) from $this->_url: ".$e->getTraceAsString());
			return array();
		}
	}
	
	public function find($query=null){
		try {
			$url=$this->_url;
			if($query) {
				$url.='?'.http_build_query(array(
					"_query"=>\sacvoyage\http\save_json($query)
				));
			}
			$response=$this->_browser->request("GET", $url);
			return new \ArrayIterator(\sacvoyage\http\load_json($response['body']));
		} catch(\sacvoyage\http\JSONException $e) {
			error_log("unable to find documents in $this->_url: ".$e->getTraceAsString());
			echo("unable to find documents in $this->_url: ".$e->getTraceAsString());
			return array();
		}
	}
	
	public function delete($query=null) {
		$url=$query 
			? $this->_url+"?"+http_build_query(array(
				"_query"=>\sacvoyage\http\save_json($query)
			))
			: $this->_url;
		$response=$this->_browser->request("DELETE", $url);
		return new \ArrayIterator(\sacvoyage\http\load_json($response['body']));
	}
	
	private $_browser;
	private $_url;
	
}


?>
